package ca.etsmtl.log430.lab3.presentation.wingui;

import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ca.etsmtl.log430.lab3.gestion.Teacher;
import ca.etsmtl.log430.lab3.gestion.TeacherAssignmentException;
import ca.etsmtl.log430.lab3.gestion.ifaces.ICommonData;
import ca.etsmtl.log430.lab3.gestion.ifaces.ICourse;
import ca.etsmtl.log430.lab3.gestion.ifaces.ITeacher;
import ca.etsmtl.log430.lab3.presentation.IDisplay;
/**
 * 
 * @author samuel
 *
 */
public class JFrameDisplay extends JFrame implements IDisplay , ActionListener
{
	/**
	 * @uml.property  name="controller"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private ICommonData controller;
	
	/**
	 * @uml.property  name="teacherVector"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="ca.etsmtl.log430.lab3.gestion.ifaces.ITeacher"
	 */
	Vector<ITeacher> teacherVector;
	/**
	 * @uml.property  name="courseVector"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="ca.etsmtl.log430.lab3.gestion.ifaces.ICourse"
	 */
	Vector<ICourse> courseVector;
	/**
	 * @uml.property  name="courseList"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="ca.etsmtl.log430.lab3.gestion.ifaces.ICourse"
	 */
	JList courseList;
	/**
	 * @uml.property  name="teacherList"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="ca.etsmtl.log430.lab3.gestion.Teacher"
	 */
	JList teacherList;
	/**
	 * @uml.property  name="selectedTeacherCourseList"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	JList selectedTeacherCourseList;
	/**
	 * @uml.property  name="assingButton"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	JButton assingButton;

	public JFrameDisplay(ICommonData controller)
			throws HeadlessException {
		super();
		this.controller = controller;
		this.setSize(500,500);
		
		teacherList = new JList();
		
		courseList = new JList();
		
		selectedTeacherCourseList = new JList();
		
		teacherVector = new Vector<ITeacher> ();
		
		courseVector = new Vector<ICourse>();
		
		
		assingButton = new JButton("Assign Course");
		
		assingButton.addActionListener(this);
		
		assingButton.setActionCommand("ASSIGN_COURSE");
		
		for (ITeacher iterable_element : controller.getListOfTeachers()) {
			teacherVector.add(iterable_element);
		}
		
		for (ICourse iterable_element : controller.getListOfCourses()) {
			courseVector.add(iterable_element);
		}
		
		
		teacherList.setListData(teacherVector);
		courseList.setListData(courseVector);
		this.setLayout(new GridLayout(2, 2));
		JScrollPane pane1 = new JScrollPane(teacherList);
		JScrollPane pane2 = new JScrollPane(courseList);
		this.add(pane1);
		this.add(pane2);
		this.add(assingButton);
		this.add(selectedTeacherCourseList);
		this.setVisible(true);
		teacherList.addListSelectionListener(new TeacherListSelection());
		courseList.addListSelectionListener(new CourseListSelection());
		
	}

	@Override
	public void displayTeacher(ITeacher teacher) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayCourse(ICourse course) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayTeachersAssignedToCourse(ICourse course) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayCoursesAssignedToTeacher(ITeacher teacher) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayTeacherList(Collection<? extends ITeacher> list) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayCourseList(Collection<? extends ICourse> list) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void displayException(TeacherAssignmentException e) {
		JOptionPane.showMessageDialog(null, e.getMessage(), "Error",  JOptionPane.ERROR_MESSAGE);
		
	}
	
	public boolean displayYesNoWarning(String question) {
		
		int answer = JOptionPane.showOptionDialog(null,
				question,
				"Confirm",
				JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE,
			    null,null,null);
		
		if( answer == 0) {
			return true;
		}
		return false;
	}

	@Override
	public void displaySigleList(Collection<String> courses) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("ASSIGN_COURSE"))
		{
			Object[] objSelectedCourses = courseList.getSelectedValues();
			
			Teacher teacher = (Teacher) teacherList.getSelectedValue();
			for (Object objCourse : objSelectedCourses) {
				try 
				{
					ICourse course = (ICourse) objCourse;
					
					boolean doAssign = true;
					if( course.getTeachers().getSize() > 0 ) {
						doAssign = displayYesNoWarning("A teacher is already assigned to this course. Do you really want to add an other one?");
					}
					if( doAssign ) {
						controller.assignTeacherToCourse(teacher, course);
					}
				} 
				catch (TeacherAssignmentException e1) {
					this.displayException(e1);
				}
			}
		}
	}

	private class TeacherListSelection implements ListSelectionListener {

		@SuppressWarnings("unchecked")
		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			
			Teacher teacher = (Teacher) teacherList.getSelectedValue();
			Vector<ICourse> assignedCourse = new Vector<ICourse>();
			for (ICourse iterable_element : teacher.getCoursesAssigned()) {
				
				assignedCourse.add(iterable_element);
				
			}
			
			selectedTeacherCourseList.setListData(assignedCourse);
			
		}
	}
	

	private class CourseListSelection implements ListSelectionListener {

		@SuppressWarnings("unchecked")
		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			
			ICourse course = (ICourse) courseList.getSelectedValue();
			Vector<ITeacher> assignedTeachers = new Vector<ITeacher>();
			
			ITeacher iterable_element;
			course.getTeachers().goToFrontOfList();
			while( (iterable_element = course.getTeachers().getNextTeacher()) != null ) {
				
				assignedTeachers.add(iterable_element);
				
			}
			
			selectedTeacherCourseList.setListData(assignedTeachers);
			
		}
	}
}
